//
//  MySlider.m
//  Exam12-1
//
//  Created by 孙晓晔 on 2017/4/23.
//  Copyright © 2017年 孙晓晔. All rights reserved.
//

#define SelectViewBgColor   [UIColor colorWithRed:9/255.0 green:170/255.0 blue:238/255.0 alpha:1]
#define defaultViewBgColor  [UIColor lightGrayColor]

#define MySlideWidth      (self.bounds.size.width)
#define MySliderHight     (self.bounds.size.height)

#define MySliderTitle_H   (MySliderHight*.3)

#define CenterImage_W       40

#define MySliderLine_W    (MySlideWidth-CenterImage_W)
#define MySLiderLine_H    6.0
#define MySliderLine_Y    (MySliderHight-MySliderTitle_H)

#define CenterImage_Y     (MySliderLine_Y+(MySLiderLine_H/2))

#import "MySlider.h"

@interface MySlider() {
    CGFloat _pointX;
    NSInteger _sectionIndex; //当前选中的那个
    CGFloat _sectionLength; //根据数组分段后一段的长度
}
/**
 *  必传，范围（0到(array.count-1)）
 */
@property (nonatomic,assign)CGFloat defaultIndx;

/**
 *  必传，传入节点数组
 */
@property (nonatomic,strong)NSArray *titleArray;

/**
 *  首，末位置的title
 */
@property (nonatomic,strong)NSArray *firstAndLastTitles;
/**
 *  传入图片
 */
@property (nonatomic,strong)UIImage *sliderImage;

@property (strong,nonatomic)UIView *defaultView;
@property (strong,nonatomic)UIImageView *centerImage;
@end

@implementation MySlider

-(instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titleArray defaultIndex:(CGFloat)defaultIndex sliderImage:(UIImage *)sliderImage {
    if (self  = [super initWithFrame:frame]) {
        _pointX = 0;
        _sectionIndex = 0;
        
        self.backgroundColor = [UIColor whiteColor];
        
        _defaultView = [[UIView alloc] initWithFrame:CGRectMake(CenterImage_W/2, MySliderLine_Y, MySlideWidth-CenterImage_W, MySLiderLine_H)];
        _defaultView.backgroundColor = defaultViewBgColor;
        _defaultView.layer.cornerRadius = MySLiderLine_H/2;
        _defaultView.userInteractionEnabled = NO;
        
        [self addSubview:_defaultView];
        
        self.titleArray = titleArray;
        self.defaultIndx = defaultIndex;
        
        _centerImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, CenterImage_W, CenterImage_W)];
        _centerImage.center = CGPointMake(0, CenterImage_Y);
        _centerImage.userInteractionEnabled = NO;
        _centerImage.backgroundColor = [UIColor whiteColor];
        _centerImage.layer.borderWidth = 1.f;
        _centerImage.layer.cornerRadius = 20;
        _centerImage.clipsToBounds = YES;
//        _centerImage.contentMode = UIViewContentModeScaleAspectFit;
//        _centerImage.alpha = .5;
        
        [self addSubview:_centerImage];
        
        self.sliderImage = sliderImage;
    }
    
    return self;
}

-(void)setDefaultIndx:(CGFloat)defaultIndx{
    CGFloat withPress = defaultIndx/(_titleArray.count-1);
    //设置默认位置
    
    _pointX = withPress*MySliderLine_W;
    _sectionIndex = defaultIndx;
}

-(void)setTitleArray:(NSArray *)titleArray{
    _titleArray = titleArray;
    
    _sectionLength = (MySliderLine_W/(titleArray.count-1));
    
    NSLog(@"(%lu),(%f),(%f)",(unsigned long)titleArray.count, MySliderLine_W, _sectionLength);
    
    for (int i = 0; i < titleArray.count; i++) {
        UILabel *label = [UILabel new];
        label.frame = CGRectMake(0, 0, 80, MySliderTitle_H);
        label.center = CGPointMake(CenterImage_W/2 + _sectionLength*i, MySliderLine_Y - MySliderTitle_H - 5);
        
//        label.backgroundColor = [UIColor greenColor];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = titleArray[i];
        
        [self addSubview:label];
        
        UIButton *btn = [UIButton new];
        btn.frame = CGRectMake(0, 0, 16, 16);
        btn.center = CGPointMake(CenterImage_W/2 + _sectionLength*i, MySliderLine_Y+2);
        
        btn.backgroundColor = [UIColor grayColor];
//        btn.t = NSTextAlignmentCenter;
        btn.layer.cornerRadius = 8;
        btn.enabled = NO;
        
        [self addSubview:btn];
    }
}

-(void)setSliderImage:(UIImage *)sliderImage{
    _centerImage.image = sliderImage;
    
    [self refreshSlider];
}

#pragma mark ---UIColor Touchu
-(BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    [self changePointX:touch];
    _pointX=_sectionIndex*(_sectionLength);
    
    [self refreshSlider];
    
    return YES;
}

- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    [self changePointX:touch];
    
    [self refreshSlider];
    [self sendActionsForControlEvents:UIControlEventValueChanged];
    
    return YES;
}

-(void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event{
    [self changePointX:touch];
    _pointX = _sectionIndex*(_sectionLength);
    
    if (self.block) {
        self.block((int)_sectionIndex);
    }
    
    [self refreshSlider];
}

-(void)changePointX:(UITouch *)touch{
    CGPoint point = [touch locationInView:self];
    _pointX = point.x;
    if (point.x < 0) {
        _pointX = CenterImage_W / 2;
    } else if (point.x > MySliderLine_W) {
        _pointX = MySliderLine_W + CenterImage_W / 2;
    }
    
    //四舍五入计算选择的节点
    _sectionIndex = (int)roundf(_pointX/_sectionLength);
    
    //NSLog(@"pointx=(%f),(%ld),(%f)",point.x,(long)_sectionIndex,_pointX);
}

-(void)refreshSlider {
    _pointX = _pointX + CenterImage_W/2;
    
    _centerImage.center = CGPointMake(_pointX, CenterImage_Y);
}
@end
